package cn.itcast.mq.listener;

import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author liudo
 * @version 1.0
 * @project mq-demo
 * @description 消费者
 * @date 2023/8/19 10:24:06
 */
@Component
@Slf4j
public class SpringRabbitListener {

    /**
     * 监听简单队列信息
     *
     * @param msg 消息
     */
    // @RabbitListener(queues = "simple.queue")
    // public void listenSimpleMessage1(String msg) throws InterruptedException {
    //     Thread.sleep(5000);
    //     log.info("listenSimpleMessage1接收到了:{}", msg);
    // }
    //
    // @RabbitListener(queues = "simple.queue")
    // public void listenSimpleMessage2(String msg) throws InterruptedException {
    //     Thread.sleep(1000);
    //     log.info("listenSimpleMessage2接收到了:{}", msg);
    // }
    //
    // @RabbitListener(queues = "simple.queue")
    // public void listenSimpleMessage3(String msg) {
    //     log.info("listenSimpleMessage3接收到了:{}", msg);
    // }

    // ----------------广播交换机监听-----------------

    /**
     * 监听FanoutMessage
     *
     * @param msg 消息
     */
    @RabbitListener(queues = "fanout.queue")
    public void listenFanoutMessage1(String msg) {
        log.info("----------listenFanoutMessage1的消息:{}---------", msg);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutMessage2(String msg) {
        log.info("----------listenFanoutMessage2的消息:{}---------", msg);
    }


    // ----------------广播交换机监听-----------------
    // ----------------定向交换机监听-----------------

    @RabbitListener(queues = "direct.queue1")
    public void listenDirectMessage1(String msg) {
        log.info("----------listenDirectMessage1:{}---------", msg);

    }

    @RabbitListener(queues = "direct.queue2")
    public void listenDirectMessage2(String msg) {
        log.info("----------listenDirectMessage2:{}---------", msg);

    }

    // ----------------定向交换机监听-----------------
    //
    // ----------------话题交换机监听-----------------

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue1"),
                    exchange = @Exchange(name = "topic.exchange", type = ExchangeTypes.TOPIC),
                    key = {"hf.*", "*.news"}
            )
    )
    public void listenTopicMessage1(String msg) throws JSONException {
        // 将收到的消息 转换成JSONArray fastjson
        JSONArray jsonArray = new JSONArray(msg);
        for (int i = 0; i < jsonArray.length(); i++) {
            int anInt = jsonArray.getInt(i);
            log.info("----------合肥的信息或新闻:{}---------", anInt);
        }

    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue2"),
                    exchange = @Exchange(name = "topic.exchange", type = ExchangeTypes.TOPIC),
                    key = {"fy.*", "*.weather"}
            ))
    public void listenTopicMessage2(String msg) {
        log.info("----------阜阳的信息或天气:{}---------", msg);

    }

    // ----------------话题交换机监听-----------------

}
